import { ListNode, transArryToLinkList, transToArry } from "./util";

export function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
    const values = transToArry(head);
    let resultArr: number[] = [];
    const len = values.length;
    const left = len % k;
    const temArr = values.slice(0, len - left);
    for (let i = 0, len = temArr.length; i < len; i++) {
        const index = i + k - 2 * (i % k) - 1;
        resultArr.push(temArr[index])
    }
    if (left) {
        resultArr = resultArr.concat(values.slice(0 - left))
    }
    return transArryToLinkList(resultArr)
};